package com.cskaoyan.service;

import com.cskaoyan.bean.ad.AdAddBO;
import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.Ad;
import com.cskaoyan.bean.domain.AdExample;
import com.cskaoyan.mapper.AdMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author:
 * @date:
 */
@Transactional
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    AdMapper adMapper;

    @Override
    public BaseData adList(String name, String content, BaseParam baseParam) {
        AdExample adExample = new AdExample();
        //开启分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        //设置排序，按照sort排序，order降序或者升序
        adExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        //如果name和content不为空，添加查询条件
        AdExample.Criteria criteria = adExample.createCriteria();
//        adExample.createCriteria().andNameLike(name).andContentLike(content).andDeletedEqualTo(0);
        if (!MyUtils.isEmpty(name))
            criteria.andNameLike("%" + name + "%");
        if (!MyUtils.isEmpty(content))
            criteria.andContentLike("%" + content + "%");
        criteria.andDeletedEqualTo(false);
        //进行查询
        List<Ad> ads = adMapper.selectByExample(adExample);
        //设置分页
        PageInfo pageInfo = new PageInfo(ads);
        BaseData baseData = new BaseData();
        baseData.setList(ads);
        baseData.setLimit(baseParam.getLimit());
        baseData.setPage(baseParam.getPage());
        baseData.setPages(pageInfo.getPages());
        baseData.setTotal((int) pageInfo.getTotal());
        return baseData;
    }

    @Override
    public Ad adAdd(AdAddBO adBo) {
        Ad ad = new Ad();
        ad.setContent(adBo.getContent());
        ad.setEnabled(adBo.getEnabled());//能把Boolean转成tinyint吗,可以
        ad.setLink(adBo.getLink());
        ad.setName(adBo.getName());
        ad.setPosition(adBo.getPosition());
        ad.setUrl(adBo.getUrl());
        adMapper.insertSelective(ad);
        return ad;
    }

    @Override
    public Ad adUpdate(AdAddBO adBO) {
        Ad ad = new Ad();
        ad.setUrl(adBO.getUrl());
        ad.setAddTime(adBO.getAddTime());
        ad.setPosition(adBO.getPosition());
        ad.setName(adBO.getName());
        ad.setLink(adBO.getLink());
        ad.setEnabled(adBO.getEnabled());
        ad.setContent(adBO.getContent());
        AdExample adExample = new AdExample();
        adExample.createCriteria().andIdEqualTo(adBO.getId());
        adMapper.updateByExampleSelective(ad, adExample);
        Ad adAfterUpdate = adMapper.selectByPrimaryKey(adBO.getId());
        return adAfterUpdate;
    }

    @Override
    public int adDelete(AdAddBO adBO) {
        Ad ad = new Ad();
        ad.setDeleted(true);
        AdExample adExample = new AdExample();
        adExample.createCriteria().andIdEqualTo(adBO.getId());
        int affectedRow = adMapper.updateByExampleSelective(ad, adExample);
        return affectedRow;
    }

    @Override
    public List<Ad> adWithOutPage() {
        //广告列表
        AdExample adExample = new AdExample();
        adExample.createCriteria().andDeletedEqualTo(false).andEnabledEqualTo(true);
        List<Ad> ads = adMapper.selectByExample(adExample);
        return ads;
    }


}
